package com.voute.View.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.voute.App.App;
import com.voute.Model.Bean.DiscoveryFeed;
import com.voute.R;
import com.voute.Utils.DisplayUtil;
import com.voute.Utils.GlideUtils;
import com.voute.Utils.ViewHolderUtils;
import com.voute.View.Activity.FeedDetailInfoActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liucxu on 2017/4/27.
 */

public class SimpleFeedAdapter extends BaseAdapter {

    private List<DiscoveryFeed> entities = new ArrayList<>();
    private Context mContext;

    public SimpleFeedAdapter(List<DiscoveryFeed> datas, Context c) {
        if (datas != null) {
            entities.clear();
            entities.addAll(datas);
            mContext = c;
        }
    }

    @Override
    public int getCount() {
        return entities.size();
    }

    @Override
    public Object getItem(int i) {
        return entities.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convert_view, ViewGroup viewGroup) {
        if (convert_view == null) {
            convert_view = View.inflate(mContext, R.layout.adapter_discovery_framelayout_circle_feed, null);
        }
        initViews(convert_view, i);
        return convert_view;
    }

    private void initViews(View convertView, int position) {
        final DiscoveryFeed feed = entities.get(position);
        ImageView adapter_discovery_imager = ViewHolderUtils.get(convertView, R.id.adapter_discovery_imager);
        TextView adapter_discovery_time = ViewHolderUtils.get(convertView, R.id.adapter_discovery_time);
        TextView adapter_discovery_vote_num = ViewHolderUtils.get(convertView, R.id.adapter_discovery_vote_num);
        TextView adapter_discovery_desc = ViewHolderUtils.get(convertView, R.id.adapter_discovery_desc);
        adapter_discovery_desc.setText(feed.getTitle());
        Drawable rightDrawable = mContext.getResources().getDrawable(R.mipmap.adapter_nearby);
        rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
        adapter_discovery_time.setText(feed.getLive_time());
        adapter_discovery_vote_num.setText(feed.getAll_vote() + "");
        if (!TextUtils.isEmpty(feed.getPic())) {
            adapter_discovery_imager.setVisibility(View.VISIBLE);
            adapter_discovery_imager.setMaxHeight(9 * (DisplayUtil.GetDisplayWidth() - DisplayUtil.dip2px(App.GetInstance(), 24)) / 16);
            GlideUtils.getInstance().LoadContextBitmap(mContext, feed.getPic(), adapter_discovery_imager, R.mipmap.item_backgroud, R.mipmap.item_backgroud, GlideUtils.LOAD_BITMAP);
        } else {
            adapter_discovery_imager.setVisibility(View.GONE);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, FeedDetailInfoActivity.class);
                intent.putExtra("KEY_FEED_HASH",feed.getFeed_hash());
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                mContext.startActivity(intent);
            }
        });
    }

    public void setData(List<DiscoveryFeed> datas) {
        if (datas != null) {
            entities.clear();
            entities.addAll(datas);
            notifyDataSetChanged();
        }
    }
}
